package pandabay2.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pandabay2.DAO.AuctionDAO;
import pandabay2.domain.Auction;
import pandabay2.domain.User;

/**
 * Servlet implementation class LoginServlet
 */
public class AuctionDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuctionDetailServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("In the Auction Detail Servlet");
			String price = "";
			int auction_id = Integer.parseInt(request.getParameter("id"));
			Auction auction = AuctionDAO.readAuction(auction_id);
			request.setAttribute("auction", auction);
			
			// check if time for aucton is up
			String endDate = auction.getEndDate();
			Date date = new Date();
			SimpleDateFormat toDate = new SimpleDateFormat("dd.MM.yyyy");
			Date simpleEndDate = toDate.parse(endDate);
			System.out.print("Date : " + date + "My EndDate: " + simpleEndDate);
			if(date.compareTo(simpleEndDate)<0) {
			
//			System.out.println("Buyer: " + auction.getBuyer());
			if(auction.getBuyer() == 0) {
				if(auction.getHighestBid() > auction.getStartPrice())
					price = "Aktuelles Gebot: " + auction.getHighestBid() + " &euro;<br/>Startpreis: <strike>" + auction.getStartPrice() + " &euro;</strike><br/>";
				else
					price = "Startpreis: " + auction.getStartPrice();
				request.setAttribute("price", price);
				request.getRequestDispatcher("/AuctionDetail.jsp").forward(request, response);
			} else {
				request.getRequestDispatcher("/AuctionSold.jsp").forward(request, response);
			}
			} else {
				request.getRequestDispatcher("/AuctionTimeUp.jsp").forward(request, response);
			}
		} catch (Throwable exc) {
			System.out.println(exc);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
